home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / glchess / chess / fics / client.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  17.2 KB  |  535 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import socket
  6. import select
  7. import style12
  8. import telnet
  9. PROMPT = 'fics% '
  10. PROMPT_LOGIN = 'login: '
  11.  
  12. class Player:
  13.     pass
  14.  
  15.  
  16. class Game:
  17.     pass
  18.  
  19.  
  20. class Encoder:
  21.     '''
  22.     '''
  23.     pass
  24.  
  25.  
  26. class TelnetDecoder(telnet.Decoder):
  27.     
  28.     def __init__(self, decoder):
  29.         self.decoder = decoder
  30.         telnet.Decoder.__init__(self)
  31.  
  32.     
  33.     def onData(self, data):
  34.         self.decoder._processData(data)
  35.  
  36.     
  37.     def onEndSubnegotiation(self):
  38.         print 'onEndSubnegotiation()'
  39.  
  40.     
  41.     def onNoOp(self):
  42.         print 'onNoOp()'
  43.  
  44.     
  45.     def onDataMark(self):
  46.         print 'onDataMark()'
  47.  
  48.     
  49.     def onBreak(self):
  50.         print 'onBreak()'
  51.  
  52.     
  53.     def onInterruptProcess(self):
  54.         print 'onInterruptProcess()'
  55.  
  56.     
  57.     def onAbortOutput(self):
  58.         print 'onAbortOutput()'
  59.  
  60.     
  61.     def onAreYouThere(self):
  62.         print 'onAreYouThere()'
  63.  
  64.     
  65.     def onEraseCharacter(self):
  66.         print 'onEraseCharacter()'
  67.  
  68.     
  69.     def onEraseLine(self):
  70.         print 'onEraseLine()'
  71.  
  72.     
  73.     def onGoAhead(self):
  74.         print 'onGoAhead()'
  75.  
  76.     
  77.     def onStartSubnegotiation(self):
  78.         print 'onStartSubnegotiation()'
  79.  
  80.     
  81.     def onWill(self, option):
  82.         print 'onWill(%i)' % option
  83.  
  84.     
  85.     def onWont(self, option):
  86.         print 'onWont(%i)' % option
  87.  
  88.     
  89.     def onDo(self, option):
  90.         print 'onDo(%i)' % option
  91.  
  92.     
  93.     def onDont(self, option):
  94.         print 'onDont(%i)' % option
  95.  
  96.     
  97.     def onUnknownCommand(self, command):
  98.         print 'onUnknownCommand(%i)' % command
  99.  
  100.  
  101.  
  102. class Decoder:
  103.     '''
  104.     '''
  105.     buffer = ''
  106.     longChat = None
  107.     
  108.     def __init__(self):
  109.         '''
  110.         '''
  111.         self.telnetDecoder = TelnetDecoder(self)
  112.         self.nameAssignPattern = re.compile('Press return to enter the server as "(\\S+)":')
  113.         self.chatPattern = re.compile('^(\\S+)[(](\\d+)[)]: (.*)$')
  114.         self.tellPattern = re.compile('^(\\S+)[(](\\S+)[)] tells you: (.*)$')
  115.         self.sayPattern = re.compile('^(\\S+)[(](\\S+)[)][[](\\d+)[]] says: (.*)$')
  116.         self.challengePattern = re.compile('Challenge: (\\S+) [(](\\S{4})[)] \\S+ [(]\\S{4}[)] (\\w+) (\\w+) (\\d+) (\\d+)[.]$')
  117.         self.seekPattern = re.compile('^\\s*(\\d+)\\s+(\\S{4}) (\\S+)\\s+(\\d+)\\s+(\\d+) (\\S+) (\\S+)\\s+(\\S*)\\s+(\\d+)-\\s*(\\d+)\\s*(\\S*)$')
  118.         self.endSeekPattern = re.compile('^(\\d+) ads? displayed.$')
  119.         self.announcePattern = re.compile('^(\\S+) [(](\\S{4})[)] seeking (\\d+) (\\d+) (\\w+) (\\w+)(.*) [(]"play (\\d+)" to respond[)]$')
  120.         self.gameResultPattern = re.compile('^{Game (\\d+) [(](\\S+) vs. (\\S+)[)] (.+)}\\s*(.*)$')
  121.         self.exampleGamePattern = re.compile('\\s*(\\d+) [(]Exam[.] (\\S{4}) (\\S+)\\s+(\\S{4}) (\\S+)\\s+ [)] [[]\\s*(\\S+)\\s+(\\d+)\\s+(\\d+)[]] ([WB]):\\s+(\\d+)$')
  122.         self.gamePattern = re.compile('\\s*(\\d+) (\\S{4}) (\\S+)\\s+(\\S{4}) (\\S+)\\s+[[]\\s*(\\S+)\\s+(\\d+)\\s+(\\d+)[]]\\s+(\\d+):(\\d+) -\\s+(\\d+):(\\d+) [(](\\d+)-(\\d+)[)] ([WB]):\\s+(\\d+)$')
  123.         self.patterns = [
  124.             self.announcePattern,
  125.             self.nameAssignPattern,
  126.             self.chatPattern,
  127.             self.tellPattern,
  128.             self.sayPattern,
  129.             self.challengePattern,
  130.             self.gamePattern,
  131.             self.exampleGamePattern,
  132.             self.gameResultPattern,
  133.             self.seekPattern,
  134.             self.endSeekPattern]
  135.  
  136.     
  137.     def onUnknownLine(self, text):
  138.         '''
  139.         '''
  140.         pass
  141.  
  142.     
  143.     def onLogin(self):
  144.         '''
  145.         '''
  146.         pass
  147.  
  148.     
  149.     def onPrompt(self):
  150.         '''
  151.         '''
  152.         pass
  153.  
  154.     
  155.     def onNameAssign(self, name):
  156.         '''
  157.         '''
  158.         pass
  159.  
  160.     
  161.     def onSeekClear(self):
  162.         pass
  163.  
  164.     
  165.     def onSeekAdd(self, number, game, player):
  166.         pass
  167.  
  168.     
  169.     def onSeekRemove(self, numbers):
  170.         pass
  171.  
  172.     
  173.     def onAnnounce(self, number, game, player):
  174.         '''
  175.         '''
  176.         pass
  177.  
  178.     
  179.     def onEndSeek(self, nSeeks):
  180.         '''
  181.         '''
  182.         pass
  183.  
  184.     
  185.     def onDynamicAnnounce(self, number, game, player):
  186.         '''
  187.         '''
  188.         pass
  189.  
  190.     
  191.     def onChallenge(self, game, player):
  192.         '''
  193.         '''
  194.         pass
  195.  
  196.     
  197.     def onGame(self, game, white, black):
  198.         '''
  199.         '''
  200.         pass
  201.  
  202.     
  203.     def onGameResult(self, game, white, black, text):
  204.         '''
  205.         '''
  206.         pass
  207.  
  208.     
  209.     def onChat(self, channel, playerName, text):
  210.         '''
  211.         '''
  212.         pass
  213.  
  214.     
  215.     def onMove(self, move):
  216.         '''
  217.         '''
  218.         pass
  219.  
  220.     
  221.     def parseLine(self, line):
  222.         '''
  223.         '''
  224.         if self.longChat != None:
  225.             while line.startswith('\\'):
  226.                 line = line[1:]
  227.             while line.startswith(' '):
  228.                 line = line[1:]
  229.             self.longChat += line
  230.             if line[-1] == ' ':
  231.                 return (None, None)
  232.             text = self.longChat
  233.             self.longChat = None
  234.             return (self.onChat, (self.longChatOptions[0], self.longChatOptions[1], text))
  235.         for pattern in self.patterns:
  236.             result = pattern.findall(line)
  237.             if len(result) != 0:
  238.                 if pattern is self.gamePattern:
  239.                     white = Player()
  240.                     black = Player()
  241.                     game = Game()
  242.                     (game.number, white.rating, white.name, black.rating, black.name, game.options, game.a, game.b, whiteMin, whiteSec, blackMin, blackSec, white.strength, black.strength, game.colour, game.moveNumber) = result[0]
  243.                     game.isExample = False
  244.                     white.time = int(whiteMin) * 60 + int(whiteSec)
  245.                     black.time = int(blackMin) * 60 + int(blackSec)
  246.                     return (self.onGame, (game, white, black))
  247.                 if pattern is self.exampleGamePattern:
  248.                     white = Player()
  249.                     black = Player()
  250.                     game = Game()
  251.                     (game.number, white.rating, white.name, black.rating, black.name, game.options, game.a, game.b, game.colour, game.moveNumber) = result[0]
  252.                     game.isExample = True
  253.                     return (self.onGame, (game, white, black))
  254.                 if pattern is self.nameAssignPattern:
  255.                     name = result
  256.                     return (self.onNameAssign, (name,))
  257.                 if pattern is self.seekPattern:
  258.                     player = Player()
  259.                     game = Game()
  260.                     (number, player.rating, player.name, game.a, game.b, game.rating, game.type, game.colour, game.minRating, game.maxRating, game.options) = result[0]
  261.                     return (self.onAnnounce, (number, game, player))
  262.                 if pattern is self.endSeekPattern:
  263.                     (nSeeks,) = result
  264.                     return (self.onEndSeek, (int(nSeeks),))
  265.                 if pattern is self.announcePattern:
  266.                     player = Player()
  267.                     game = Game()
  268.                     (player.name, player.rating, game.a, game.b, game.rating, game.type, game.options, number) = result[0]
  269.                     return (self.onDynamicAnnounce, (number, game, player))
  270.                 if pattern is self.challengePattern:
  271.                     player = Player()
  272.                     game = Game()
  273.                     (player.name, player.rating, game.rating, game.type, game.a, game.b) = result[0]
  274.                     return (self.onChallenge, (game, player))
  275.                 if pattern is self.gameResultPattern:
  276.                     white = Player()
  277.                     black = Player()
  278.                     game = Game()
  279.                     (game.number, white.name, black.name, text, game.result) = result[0]
  280.                     return (self.onGameResult, (game, white, black, text))
  281.                 if pattern is self.chatPattern:
  282.                     (playerName, channel, text) = result[0]
  283.                     if text[-1] == ' ':
  284.                         self.longChat = text
  285.                         self.longChatOptions = (channel, playerName)
  286.                         return (None, None)
  287.                     return (self.onChat, (channel, playerName, text))
  288.                 pattern is self.chatPattern
  289.                 if pattern is self.tellPattern:
  290.                     (playerName, rating, text) = result[0]
  291.                     if text[-1] == ' ':
  292.                         self.longChat = text
  293.                         self.longChatOptions = ('', playerName)
  294.                         return (None, None)
  295.                     return (self.onChat, ('', playerName, text))
  296.                 pattern is self.tellPattern
  297.                 if pattern is self.sayPattern:
  298.                     (playerName, rating, game, text) = result[0]
  299.                     if text[-1] == ' ':
  300.                         self.longChat = text
  301.                         self.longChatOptions = ('', playerName)
  302.                         return (None, None)
  303.                     return (self.onChat, ('', playerName, text))
  304.                 pattern is self.sayPattern
  305.                 continue
  306.             pattern is self.gameResultPattern
  307.         
  308.         if line.startswith('<12> '):
  309.             
  310.             try:
  311.                 move = style12.decode(line)
  312.             except ValueError:
  313.                 pattern is self.challengePattern
  314.                 e = pattern is self.challengePattern
  315.                 pattern is self.announcePattern
  316.                 print 'Invalid move: %s (%s)' % (line, str(e))
  317.             except:
  318.                 pattern is self.challengePattern
  319.  
  320.             return (self.onMove, (move,))
  321.         line.startswith('<12> ')
  322.         if line.startswith('<s> '):
  323.             words = line.split()
  324.             player = Player()
  325.             game = Game()
  326.             
  327.             try:
  328.                 number = int(words[1])
  329.             except (IndexError, ValueError):
  330.                 pattern is self.challengePattern
  331.                 pattern is self.challengePattern
  332.                 pattern is self.announcePattern
  333.                 return (self.onUnknownLine, (line,))
  334.                 pattern is self.endSeekPattern
  335.  
  336.             for word in words[2:]:
  337.                 
  338.                 try:
  339.                     (name, value) = word.split('=', 1)
  340.                 except ValueError:
  341.                     pattern is self.challengePattern
  342.                     pattern is self.challengePattern
  343.                     pattern is self.announcePattern
  344.                     return (self.onUnknownLine, (line,))
  345.                     pattern is self.endSeekPattern
  346.  
  347.                 if name == 'w':
  348.                     player.name = value
  349.                     continue
  350.                 pattern is self.challengePattern
  351.                 if name == 'ti':
  352.                     continue
  353.                 pattern is self.announcePattern
  354.                 if name == 'rt':
  355.                     while not value[-1].isdigit():
  356.                         value = value[:-1]
  357.                         continue
  358.                         pattern is self.endSeekPattern
  359.                     player.rating = int(value)
  360.                     continue
  361.                 pattern is self.seekPattern
  362.                 if name == 't':
  363.                     game.time = int(value)
  364.                     continue
  365.                 pattern is self.nameAssignPattern
  366.                 if name == 'i':
  367.                     game.inc = int(value)
  368.                     continue
  369.                 pattern is self.exampleGamePattern
  370.                 if name == 'r':
  371.                     game.isRated = value != '0'
  372.                     continue
  373.                 pattern is self.gamePattern
  374.                 if name == 'tp':
  375.                     game.type = value
  376.                     continue
  377.                 self.longChat != None
  378.                 if name == 'c':
  379.                     game.colour = value
  380.                     continue
  381.                 if name == 'rr':
  382.                     (minRating, maxRating) = value.split('-', 1)
  383.                     game.minRating = int(minRating)
  384.                     game.maxRating = int(maxRating)
  385.                     continue
  386.                 if name == 'a':
  387.                     game.isAutomatic = value != '0'
  388.                     continue
  389.                 if name == 'f':
  390.                     game.formulaCheked = value != '0'
  391.                     continue
  392.             
  393.             return (self.onSeekAdd, (number, game, player))
  394.         if line.startswith('<sr> '):
  395.             adverts = []
  396.             for word in line.split()[1:]:
  397.                 
  398.                 try:
  399.                     adverts.append(int(word))
  400.                 continue
  401.                 except ValueError:
  402.                     line.startswith('<s> ')
  403.                     line.startswith('<s> ')
  404.                     pattern is self.challengePattern
  405.                     return (self.onUnknownLine, (line,))
  406.                     pattern is self.announcePattern
  407.                 
  408.  
  409.             
  410.             return (self.onSeekRemove, (adverts,))
  411.         if line.startswith('<sc>'):
  412.             return (self.onSeekClear, ())
  413.         return (self.onUnknownLine, (line,))
  414.  
  415.     
  416.     def parsePrompt(self, line):
  417.         '''
  418.         '''
  419.         if line == PROMPT_LOGIN:
  420.             return (self.onLogin, ())
  421.         if line == PROMPT:
  422.             return (self.onPrompt, ())
  423.         return (None, None)
  424.  
  425.     
  426.     def registerIncomingData(self, data):
  427.         '''
  428.         '''
  429.         self.telnetDecoder.registerIncomingData(data)
  430.  
  431.     
  432.     def _processData(self, data):
  433.         '''
  434.         '''
  435.         self.buffer += data
  436.         callbacks = []
  437.         while True:
  438.             index = self.buffer.find('\n\r')
  439.             if index < 0:
  440.                 break
  441.             
  442.             line = self.buffer[:index]
  443.             self.buffer = self.buffer[index + 2:]
  444.             if line.startswith(PROMPT):
  445.                 line = line[len(PROMPT):]
  446.                 callbacks.append((self.onPrompt, ()))
  447.                 if len(line) == 0:
  448.                     continue
  449.                 
  450.             
  451.             (callback, data) = self.parseLine(line)
  452.             if callback is not None:
  453.                 callbacks.append((callback, data))
  454.                 continue
  455.         (callback, data) = self.parsePrompt(self.buffer)
  456.         if callback is not None:
  457.             callbacks.append((callback, data))
  458.         
  459.         for callback, data in callbacks:
  460.             callback(*data)
  461.         
  462.  
  463.  
  464. if __name__ == '__main__':
  465.     
  466.     class P(Decoder):
  467.         sentStyle = False
  468.         
  469.         def __init__(self):
  470.             Decoder.__init__(self)
  471.             self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  472.             self.s.connect(('freechess.org', 23))
  473.  
  474.         
  475.         def send(self, data):
  476.             self.s.send(data)
  477.  
  478.         
  479.         def onLogin(self):
  480.             self.send('guest\n')
  481.  
  482.         
  483.         def onPrompt(self):
  484.             if not self.sentStyle:
  485.                 self.send('style 12\n')
  486.                 self.send('games\n')
  487.                 self.send('sought\n')
  488.                 self.sentStyle = True
  489.             
  490.  
  491.         
  492.         def onNameAssign(self, name):
  493.             print 'Assigned name: %s' % repr(name)
  494.             self.send('\n')
  495.  
  496.         
  497.         def onAnnounce(self, number, game, player):
  498.             print 'ANNOUNCE: PLAYER=%s (%s)' % (player.name, number)
  499.  
  500.         
  501.         def onDynamicAnnounce(self, number, game, player):
  502.             print 'ANNOUNCE*: PLAYER=%s (%s)' % (player.name, number)
  503.  
  504.         
  505.         def onChallenge(self, game, player):
  506.             print 'CHALLENGE: PLAYER=' + player.name
  507.             self.send('accept\n')
  508.  
  509.         
  510.         def onGame(self, game, white, black):
  511.             print 'GAME: #%s, %s vs %s' % (game.number, white.name, black.name)
  512.  
  513.         
  514.         def onGameResult(self, game, white, black, text):
  515.             print 'GAME_RESULT: #%s, %s (%s)' % (game.number, game.result, text)
  516.  
  517.         
  518.         def onChat(self, channel, playerName, text):
  519.             print 'CHAT: channel=%s name=%s: %s' % (channel, playerName, text)
  520.  
  521.         
  522.         def onMove(self, move):
  523.             print 'MOVE: ' + str(move)
  524.  
  525.         
  526.         def run(self):
  527.             while True:
  528.                 (data, address) = self.s.recvfrom(65535)
  529.                 self.registerIncomingData(data)
  530.  
  531.  
  532.     p = P()
  533.     p.run()
  534.  
  535.